package com.dd.cloud.search.controller;

import cn.easyes.core.biz.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.search.entity.DrugsEs;
import com.dd.cloud.search.service.DrugsEsService;
import com.dd.cloud.user.req.pres.PresPageReq;
import com.dd.cloud.user.res.pres.DrugsModelPageRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 药品控制器
 *
 * @author zk
 */
@RestController
@RequestMapping("/drugs")
@Validated
@Slf4j
public class DrugsEsController {
    @Autowired
    private DrugsEsService drugsEsService;

    /**
     * es批量保存药材列表数据
     *
     * @param esModelList 药品列表
     * @return 导入经验方数据
     */
    @PostMapping("/es/save")
    public ResponseEntity<Boolean> esSave(@RequestBody List<DrugsModelPageRes> esModelList) {
        Boolean save = drugsEsService.esSave(esModelList);
        return ResponseEntity.ok(save);
    }


    /**
     * es分页搜索药品
     *
     * @param req 参数
     * @return 分页数据
     */
    @GetMapping("/es/query/page")
    public ResponseEntity<PageInfo<DrugsEs>> queryPage(@Valid PresPageReq req) {
        PageInfo<DrugsEs> page = drugsEsService.queryPage(req);
        return ResponseEntity.ok(page);
    }

    /**
     * es添加或更新药品
     *
     * @param req 参数 id存在则更新, 不存在则添加
     * @return 分页数据
     */
    @PostMapping("/es/addOrUpdate")
    public ResponseEntity<Integer> addOrUpdate(@Valid @RequestBody DrugsModelPageRes req) {
        int i = drugsEsService.updateDrugsById(req);
        return ResponseEntity.ok(i);
    }

    /**
     * es id搜索药品
     *
     * @param id 参数
     * @return 分页数据
     */
    @GetMapping("/es/query/id")
    public ResponseEntity<DrugsEs> queryById(Integer id) {
        DrugsEs drugsEs = drugsEsService.getDrugsById(id);
        return ResponseEntity.ok(drugsEs);
    }

    /**
     * es药品名搜索药品
     *
     * @param names 药品名,多个英文逗号隔开
     * @param pharId 药房id
     * @return 分页数据
     */
    @GetMapping("/es/query/names")
    public ResponseEntity<Map<String, Object>> queryNames(String names, Integer pharId) {
        Map<String, Object> drugsEs = drugsEsService.getDrugsByNames(names, pharId);
        return ResponseEntity.ok(drugsEs);
    }

    /**
     * es删除药品
     *
     * @param id 药品id
     * @return 分页数据
     */
    @PostMapping("/es/del/id")
    public ResponseEntity<Integer> delById(Integer id) {
        int i = drugsEsService.delDrugsById(id);
        return ResponseEntity.ok(i);
    }
}
